<script setup>
import {ref} from 'vue';
import {useRouter} from 'vue-router';
import {ElMessageBox} from 'element-plus';
import {getDepartmentTreeApi, getUserListApi} from "@/service/user.js";

const emits = defineEmits(['handleShare']);

// 引入分页组件
import CommonPagination from '@/components/pagination.vue';

const router = useRouter();
// 过滤条件
const filterForm = ref({
  keyword: '',
  status: '',
  role_id: '',
  department_id: ''
});

// 分页数据
const pagination = reactive({
  page: 1,
  limit: 15,
  total: 0
});

// 获取用户列表
const tableData = ref([]);
const fetchUserList = () => {
  let params = {
    ...filterForm.value,
    pagination
  }
  getUserListApi(params).then(res => {
    tableData.value = res.data.list;
    pagination.total = res.data.pagination.total;
  });
};

// 搜索
const handleSearch = () => {
  pagination.page = 1;
  fetchUserList();
};

// 重置
const handleReset = () => {
  filterForm.value = {
    keyword: '',
    status: '',
    role_id: '',
    department_id: ''
  };
  handleSearch();
};


// 获取部门树
const treeData = ref([]);
const fetchDepartmentTree = () => {
  getDepartmentTreeApi().then(res => {
    treeData.value = res.data.tree;
  });
};


// 按照部门筛选用户
const filterUserByDpt = (id) => {
  filterForm.value.department_id = id;
  handleSearch();
};

// 已选中的用户
const selectedUser = ref([]);

// 选择用户
const handleSelectionChange = (val) => {
  selectedUser.value = val.map(item => item.id);
};

const visible = ref(false);
// 打开弹窗
const openDialog = () => {
  selectedUser.value = [];
  visible.value = true;
  filterForm.value = {
    keyword: '',
    status: '',
    role_id: '',
    department_id: ''
  };
  // 获取部门树
  fetchDepartmentTree();
  // 获取用户列表
  fetchUserList();
};

// 关闭弹窗
const closeDialog = () => {
  visible.value = false;
  // 重置搜索条件
  filterForm.value = {
    keyword: '',
    status: '',
    role_id: '',
    department_id: ''
  };
};

// 执行分享
const handleShare = () => {
  if (selectedUser.value.length === 0) {
    ElMessageBox.alert('请选择要分享的用户', '提示', {
      type: 'warning'
    });
    return;
  }

  emits('handleShare', selectedUser.value);
  closeDialog();
};


defineExpose({
  openDialog
});
</script>

<template>
  <el-dialog title="分配客资" v-model="visible" width="50%" @close="closeDialog">
    <div class="flex-main">
      <div class="flex-left">
        <el-tree
            :data="treeData"
            node-key="id"
            :expand-on-click-node="false"
            default-expand-all
        >
          <template v-slot="{ node, data }">
            <div class="custom-tree-node">
              <div class="title" @click.stop="filterUserByDpt(node.id)">{{ node.label }}</div>
            </div>
          </template>
        </el-tree>
      </div>
      <div class="flex-right">
        <div class="toolbar">
          <el-select v-model="filterForm.role_id" placeholder="用户角色">
            <el-option label="管理员" value="1"></el-option>
            <el-option label="普通用户" value="2"></el-option>
          </el-select>
          <el-select v-model="filterForm.status" placeholder="在职状态">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
          <el-input v-model="filterForm.keyword" placeholder="请输入关键字"></el-input>
          <el-button type="primary" @click="handleSearch" icon="Search">搜索</el-button>
          <el-button type="warning" @click="handleReset">重置</el-button>
        </div>
        <el-table class="list" :data="tableData" border style="width: 100%" row-key="id" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column prop="id" label="编号" align="center" width="55"></el-table-column>
          <el-table-column prop="nickname" label="昵称" width="120"></el-table-column>
          <el-table-column prop="realname" label="姓名" ></el-table-column>
          <el-table-column label="管理性质" width="120" align="center">
            <template #default="{ row }">
              <el-tag v-if="row.user_type == 1" type="success">普通用户</el-tag>
              <el-tag v-else-if="row.user_type == 2" type="primary">管理员</el-tag>
              <el-tag v-else type="info">外部用户</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="在职状态" width="120" align="center">
            <template #default="{ row }">
              <el-tag v-if="row.status == 0" type="warning">已离职</el-tag>
              <el-tag v-else-if="row.status == 1" type="primary">在职</el-tag>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination">
          <div class="btn-group">
            <el-button type="primary" @click="handleShare">分享</el-button>
          </div>
          <CommonPagination :pagination="pagination" @pagination="fetchUserList"/>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<style scoped lang="less">
.flex-main {
  display: flex;
  flex-direction: row;
  width: 100%;
  overflow: hidden;

  .flex-left {
    flex: 0 20%;
  }

  .flex-right {
    flex: 1;
    padding-left: 10px;
  }
}

.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
